<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>添加菜单</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="__UI__/lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="__UI__/lib/font-awesome-4.7.0/css/font-awesome.min.css" media="all">
    <link rel="stylesheet" href="__UI__/css/public.css" media="all">
    <style>
        body {
            background-color: #ffffff;
        }
    </style>
</head>
<body>
<div class="layui-form layuimini-form">

    <div style="display: inline-block; width: 350px; height:400px; padding: 10px; border: 1px solid #ddd; overflow: auto;">
        <ul id="authTree" lay-filter="authTree"></ul>
    </div>

    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">确认保存</button>
        </div>
    </div>
</div>
<script src="__UI__/lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script src="__UI__/js/lay-config.js?v=1.0.4" charset="utf-8"></script>
<script src="__UI__/js/common.js" charset="utf-8"></script>
<script>
    layui.use(['form','tree'], function () {
        var form = layui.form,
            layer = layui.layer,
            tree = layui.tree,
            $ = layui.$;


        /**
         * 获取tree数据
         * @param id
         * @returns {*[]}
         */
        function getTree(id) {
            var data = [];
            $.ajax({
                url:'{:url("admin/group/getAuthTree")}',
                type:'post',
                dataType:'json',
                async: false,
                data: {'id':id},
                success: function (res) {
                    data = res;
                }
            });
            return data;
        }

        /**
         * 初始化tree
         */
        tree.render({
            elem: '#authTree', //指定元素
            id:'authTree',
            contentType: 'application/json',
            showCheckbox: true,     //是否显示复选框
            onlyIconControl: true,
            click: function(item){ //点击节点回调
                layer.msg('当前节名称：'+ item.name + '<br>全部参数：'+ JSON.stringify(item));
                console.log(item);
            },
            data:  getTree('{$id}')
        });

        /**
         * 获取选中节点的id
         * @param nodes
         * @param ids
         * @returns {*[]}
         */
        function getNodeId(nodes,ids=[]) {
            $.each(nodes,function (index,item) {
                if(item.children) getNodeId(item.children,ids);
                else ids.push(item.id);
            });
            return ids;
        }

        /**
         * 设置默认选中节点
         */
        $.each('{$ids}'.split(','),function (key,item) {
            if (item) tree.setChecked('authTree',parseInt(item));
        });


        //监听提交
        form.on('submit(saveBtn)', function () {
            var nodes = tree.getChecked('authTree');
            postSubmit(
                $,
                "{:url('admin/group/auth')}",
                {
                    'id':'{$id}',
                    "ids": getNodeId(nodes).toString()
                },
                function (res) {
                    var iframeIndex = parent.layer.getFrameIndex(window.name);
                    parent.layer.close(iframeIndex);
                    parent.layui.table.reload('currentTableId');//刷新表格的数据
                },  //请求成功时的处理
                function () {} //请求错误时的处理
            )
            return false;
        });

    });
</script>
</body>
</html>